﻿--[[
   @brief    dirui_view
   @author   amadeusyu
   @version  1.0
   @date     2016-03-07
--]]
dirui_view_ui_path = {
    dirui_layout = "wegame_dir_ui\\dirui.tpf";
    common_item_layout = "wegame_dir_ui\\common_item.tpf/TenioUILayout/UIObject/";
    loading = "pnlLoading";
    mainUI = "pnlMain";
    hover_big_btn = "hoverBtnBig";
    hover_big_content = "hoverBtnBig\\txt_content";
    hover_small_btn = "hoverBtnSmall";
    hover_small_content = "hoverBtnSmall\\txt_content";
    scroll_container = "scrollMain\\scrollContainer";
    history_UO = "scrollMain\\scrollContainer\\uoHistory";
    isp_UO = "scrollMain\\scrollContainer\\uoIsp";
    area_UO = "scrollMain\\scrollContainer\\uoArea";
    zone_UO = "scrollMain\\scrollContainer\\uoZone";
    status_List = "scrollMain\\scrollContainer\\listStatus\\Table1";

    -- 显示状态 begin
    status_btn = "btn_status";
    -- 显示状态  end

    -- 历史登录信息 begin
    history_list = "list_history\\Table1";
    history_title = "txt_history_title";
    -- 历史登录信息 end

    -- isp列表信息 begin
    isp_list_container = "list_isp";
    isp_list = "list_isp\\Table1";
    isp_item="btn_isp_item";
    isp_item_selected = "btn_isp_item\\icon_isp_selected";
    -- isp列表信息 end

    -- area列表信息  begin
    area_list = "list_area\\Table1";
    -- area列表信息  end

    -- common_item begin
    common_item_btn = "btn_item";
    common_item_name = "btn_item\\txt_item_name";
    common_item_server_icon = "btn_item\\server_status_icon";
    common_item_ping = "btn_item\\txt_ping";
    common_item_select_icon = "btn_item\\ico_select";
    common_item_suggest_icon = "btn_item\\suggest_icon";
    common_item_lock_icon = "btn_item\\lock_icon";
    -- common_item end

    -- zone列表信息  begin
    zone_list_container = "list_zone";
    zone_list = "list_zone\\Table1";
    zone_title = "txt_zone_title";
    zone_horz_list = "list_zone_horz";
    -- zone列表信息  end

    -- role info begin
    role_list_container = "list_roleinfo";
    role_list = "list_roleinfo\\Table1";
    role_item = "btn_roleinfo";
    -- role info end

}
dirui_view_constant = {
    btn_height = 56;
    btn_width = 176;
    zone_horz_item_height = 104;
    btn_selected_bk = "9IMAGES:client_dirui\\button_selected.png?0,0,10,10;client_dirui\\button_selected.png?11,0,21,10;client_dirui\\button_selected.png?22,0,32,10;client_dirui\\button_selected.png?0,11,10,21;client_dirui\\button_selected.png?11,11,21,21;client_dirui\\button_selected.png?22,11,32,21;client_dirui\\button_selected.png?0,22,10,32;client_dirui\\button_selected.png?11,22,21,32;client_dirui\\button_selected.png?22,22,32,32;Stretch;0;";
    btn_normal_bk = "9IMAGES:client_dirui\\button_normal.png?0,0,10,10;client_dirui\\button_normal.png?11,0,21,10;client_dirui\\button_normal.png?22,0,32,10;client_dirui\\button_normal.png?0,11,10,21;client_dirui\\button_normal.png?11,11,21,21;client_dirui\\button_normal.png?22,11,32,21;client_dirui\\button_normal.png?0,22,10,32;client_dirui\\button_normal.png?11,22,21,32;client_dirui\\button_normal.png?22,22,32,32;Stretch;0;";
    btn_hover_bk = "9IMAGES:client_dirui\\button_hover.png?0,0,10,10;client_dirui\\button_hover.png?11,0,21,10;client_dirui\\button_hover.png?22,0,32,10;client_dirui\\button_hover.png?0,11,10,21;client_dirui\\button_hover.png?11,11,21,21;client_dirui\\button_hover.png?22,11,32,21;client_dirui\\button_hover.png?0,22,10,32;client_dirui\\button_hover.png?11,22,21,32;client_dirui\\button_hover.png?22,22,32,32;Stretch;0;";

    btn_isp_selected_bk = "ONEIMAGE:client_dirui\\isp_selected.png;Stretch;null;";
    btn_isp_normal_bk = "NONE:";

    server_tatus_icon = {"ONEIMAGE:client_dirui\\server_gray.png", "ONEIMAGE:client_dirui\\server_green.png", "ONEIMAGE:client_dirui\\server_yellow.png", "ONEIMAGE:client_dirui\\server_red.png"};

    suggest_bk_icon = "ONEIMAGE:client_dirui\\suggest.png;Stretch;0;";
    hot_bk_icon = "ONEIMAGE:client_dirui\\hot.png;Stretch;0;";
    new_bk_icon = "ONEIMAGE:client_dirui\\new_add.png;Stretch;0;";
}
dirui_view = {

    

    dw_view_id = 0;
    dw_main_id = 0;

    p_area_list = nil;
    p_zone_list = nil;
    p_history_list = nil;
    p_isp_list = nil;
    p_roleinfo_list = nil;
    is_reopen_by_new_game = false;


    init = function()
        base_utility.log("[dirui_view] init success.");
    end;

    un_init = function()
        shadow_helper.destroy_shadow(dirui_view.dw_view_id);
        dirui_view.p_area_list = nil;
        dirui_view.p_zone_list = nil;
        dirui_view.p_history_list = nil;
        dirui_view.p_isp_list = nil;
        dirui_view.dw_view_id = 0;
        dirui_view.p_roleinfo_list = nil;
        dirui_view.dw_view_id = 0;
    end;

    show_dir_wnd = function(caption)
        -- body
        if dirui_view.dw_view_id == 0 then
            dirui_view.dw_view_id = layout_mgr.restore_layout(dirui_view_ui_path.dirui_layout, false);
            shadow_helper.create_shadow(dirui_view.dw_view_id);
        end;
        -- 置顶
        local p_window = ui_utility.get_ctrl_ptr_by_id(dirui_view.dw_view_id);
        if p_window ~= nil then
            -- p_window:Restore();
            if nil ~= caption and "" ~= caption then
                local title_bar = ui_utility.get_ctrl_id_by_path(p_window:GetControlID(),
                    "Title_bar\\Txt_title");
                caption = caption .. " - " .. LoadStr("ID_TPF_SELECT_SERVER");
                ui_utility.setproperty_by_id(title_bar, "Caption", caption);
            end
            p_window:MoveToCenter();
            p_window:Show(TRUE);
            p_window:BringToTop();
        end;
        dirui_view.show_loading(true);
        dirui_view.show_dir_panel(false);
    end;

    -- 显示/隐藏loading
    show_loading = function(is_show)
        -- body
        if is_show then
            ui_utility.setproperty_by_path(dirui_view.dw_view_id, dirui_view_ui_path.loading, "Visible", "true");
        else
            ui_utility.setproperty_by_path(dirui_view.dw_view_id, dirui_view_ui_path.loading, "Visible", "false");
        end;
    end;

    -- 显示/隐藏主面板(包含数据)
    show_dir_panel = function(is_show)
        -- 获得pnlMain的id
        dirui_view.dw_main_id = ui_utility.get_ctrl_id_by_path(dirui_view.dw_view_id, dirui_view_ui_path.mainUI);
        if is_show then
            ui_utility.setproperty_by_path(dirui_view.dw_view_id, dirui_view_ui_path.mainUI, "Visible", "true");
        else
            ui_utility.setproperty_by_path(dirui_view.dw_view_id, dirui_view_ui_path.mainUI, "Visible", "false");
        end;
    end;

    -- 初始化显示列表
    show_default_list_ui = function()
        if dirui_view.is_valid_id(dirui_view.dw_main_id) then
            dirui_view.show_status_tip_list();
            dirui_view.update_history_list_ui();
            dirui_view.update_roleinfo_list_ui();
            dirui_view.update_isp_list_ui();
            dirui_view.update_area_list_ui();
            dirui_view.update_zone_list_ui();
            dirui_view.is_reopen_by_new_game = false;
        else
            base_utility.log("[dirui_view]show_default_list_ui, get pnlMain failed, id is illegal");
        end;
    end;

    -- 更新历史区服ui
    update_history_list_ui = function()
        if #(dirui_model.h_info_list) > 0 then
            -- 显示历史大区
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO, "Visible", "true");
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO, "Height", 117);
            dirui_view.generate_history_list(true);
        elseif #(dirui_model.picked_info_list) > 0 then
            -- 显示推荐大区
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO, "Visible", "true");
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO, "Height", 117);
            dirui_view.generate_history_list(false);
        else
            -- 不显示历史\推荐大区
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO, "Height", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO, "Visible", "false");
        end;
    end;
    -- 生成历史大区列表
    generate_history_list = function (is_history)
        local dw_history_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO);
        if dirui_view.is_valid_id(dw_history_uo) == false then
            base_utility.log("[dirui_view]generate_history_list, dw_history_uo is illegal");
            return;
        end;
        if is_history == false then
            -- 修改名称
            ui_utility.setproperty_by_path(dw_history_uo, dirui_view_ui_path.history_title, "Caption", LoadStr("DIR_SUGGEST_SERVER"));
            -- 获得列数
            local cole_num = #(dirui_model.picked_info_list);
            if cole_num > 4 then
                cole_num = 4;
            end;
            dirui_view.p_history_list = dirui_view.generate_common_table(dirui_view.p_history_list, dw_history_uo, dirui_view_ui_path.history_list, "wegame_dir_ui\\history_item.tpf/TenioUILayout/UIObject/", cole_num, dirui_view_constant.btn_width, dirui_model.picked_info_list, "history");
        else
            -- 修改名称
            ui_utility.setproperty_by_path(dw_history_uo, dirui_view_ui_path.history_title, "Caption", LoadStr("DIR_HISTORY_SERVER"));
            -- 获得列数
            local cole_num = #(dirui_model.h_info_list);
            if cole_num > 4 then
                cole_num = 4;
            end;
            dirui_view.p_history_list = dirui_view.generate_common_table(dirui_view.p_history_list, dw_history_uo, dirui_view_ui_path.history_list, "wegame_dir_ui\\history_item.tpf/TenioUILayout/UIObject/", cole_num, dirui_view_constant.btn_width, dirui_model.h_info_list, "history");
        end;

    end;

    -- 生成历史大区item
    generate_history_item = function(dw_item_id, item_info)
        if dirui_view.is_valid_id(dw_item_id) then
            -- 设置名称
            dirui_view.generate_item_name(dw_item_id, item_info);
            dirui_view.generate_zone_item_special(dw_item_id, item_info);

        else
            base_utility.log("[dirui_view]generate_history_item, dw_item_id is illegal");
        end;
    end;

    -- 更新roleinfo
    update_roleinfo_list_ui = function()
        -- body
        local dw_history_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.history_UO);
        if #(dirui_model.role_info_list) > 0  and #(dirui_model.h_info_list) > 0 then
            dirui_view.generate_roleinfo_list(dw_history_uo);
            ui_utility.setproperty_by_path(dw_history_uo, dirui_view_ui_path.role_list_container, "Visible", "true");
        else
            ui_utility.setproperty_by_path(dw_history_uo, dirui_view_ui_path.role_list_container, "Visible", "false");
        end;
    end;
    -- 生成role info list
    generate_roleinfo_list = function(dw_history_uo)
        -- 获得列数
        local colum_num = #(dirui_model.role_info_list);
        if colum_num > #(dirui_model.h_info_list) then
            colum_num = #(dirui_model.h_info_list);
        end;
        if colum_num == 0 then
            colum_num = 1;
        end;
        if colum_num > 4 then
            colum_num = 4;
        end;

        if dirui_view.p_roleinfo_list == nil then
            dirui_view.p_roleinfo_list = tolua.cast(ui_utility.get_ctrl_ptr_by_path(dw_history_uo, dirui_view_ui_path.role_list), "Tenio::ITable");
        end;

        if dirui_view.p_roleinfo_list ~= nil then
            -- 遍历添加item
            if dirui_view.p_roleinfo_list:GetColumnCount() == 0 then
                -- 插入布局文件
                local const_ui_str, str_holder = U8toCUIStr("wegame_dir_ui\\roleinfo_item.tpf/TenioUILayout/UIObject/");
                local const_null_str, str_null_holder = U8toCUIStr("");
                local append_result = 0;
                for i = 1, colum_num do
                    append_result = dirui_view.p_roleinfo_list:AppendCustomColumn(dirui_view_constant.btn_width, const_ui_str, const_null_str, 0);
                end;
            end;
            dirui_view.p_roleinfo_list:DeleteAll();
            local row_num = math.ceil(#(dirui_model.role_info_list)/colum_num);
            dirui_view.p_roleinfo_list:AppendRowCount(row_num, false);
            for row = 1, row_num do
                for col = 1, colum_num do
                    local p_item_root = dirui_view.p_roleinfo_list:GetItemTopControlPtr(row - 1, col - 1);
                    local dw_item_root = p_item_root:GetControlID();
                    local item_index = (row - 1) * 4 + col;
                    if item_index > 0 and item_index <= #(dirui_model.h_info_list) then
                        local has_role = dirui_view.generate_roleinfo_item(dw_item_root, item_index);
                        if has_role then
                            p_item_root:Show(TRUE);
                            -- 到这里，存在历史大区有角色信息的情况，设置history的高度
                            dirui_view.dw_height_history = 116;
                        else
                            p_item_root:Show(FALSE);
                        end;
                    else
                        p_item_root:Show(FALSE);
                    end;
                    
                end;
            end;
        else
            base_utility.log("[dirui_view]generate_roleinfo_table, dirui_view.p_roleinfo_list is nil");
        end;
    end;
    -- 生成role info item
    generate_roleinfo_item = function(dw_item_root, item_index)
        -- 从history列表中获得zone_id
        local h_num = #(dirui_model.h_info_list);
        local zone_id = 0;
        if h_num > 0 then
            for i = 1, h_num do
                if i == item_index then
                    zone_id = dirui_model.h_info_list[i].zone_id;
                    break;
                end;
            end;
        end;
        if zone_id > 0 then
            local role_info = {};
            -- 查找zone_id,得到待显示的角色信息
            for i = 1, #(dirui_model.role_info_list) do
                if dirui_model.role_info_list[i].zoneId == zone_id then
                    role_info.role_name = dirui_model.role_info_list[i].roleName;
                    role_info.role_level = dirui_model.role_info_list[i].roleLevel;
                    break;
                end;
            end;
            if role_info.role_name then
                -- 找到了一个role_name，设置到界面上
                ui_utility.setproperty_by_path(dw_item_root, dirui_view_ui_path.role_item, "Caption", role_info.role_name);
                ui_utility.setproperty_by_path(dw_item_root, dirui_view_ui_path.role_item, "Icon", "client_dirui\\user_tag.png");
                return true;
            end;
        end;
        -- 没有找到名称，则返回false
        return false;
    end;
    -- 更新zone ui
    update_zone_list_ui = function()
        if dirui_control.should_show_horz_zone_list() then
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Visible", "true");
            dirui_view.show_vert_zone_list(false);
            dirui_view.set_horz_zone_height();
            dirui_view.generate_horz_zone_list();
            dirui_view.update_ping_value(dirui_model.zone_list_toshow);
            return;
        else
            dirui_view.show_vert_zone_list(true);
        end

        if #(dirui_model.zone_list_toshow) > 0 then
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Visible", "true");
            -- 设置高度和offsety
            dirui_view.set_zone_height();
            dirui_view.generate_zone_list();
            dirui_view.update_ping_value(dirui_model.zone_list_toshow);
        else
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Height", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Visible", "false");
        end;
    end;

    generate_zone_list = function()
        local dw_zone_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO);
        if dirui_view.is_valid_id(dw_zone_uo) == false then
            base_utility.log("[dirui_view]generate_zone_list, dw_zone_uo is illegal");
            return;
        end;
        -- 生成大区列表

        dirui_view.p_zone_list = dirui_view.generate_common_table(dirui_view.p_zone_list, dw_zone_uo, dirui_view_ui_path.zone_list, "wegame_dir_ui\\common_item.tpf/TenioUILayout/UIObject/", 4, 176, dirui_model.zone_list_toshow, "zone");
    end;

    generate_zone_item = function(dw_item_id, item_info)
        if dirui_view.is_valid_id(dw_item_id) then
            -- 设置名称
            dirui_view.generate_item_name(dw_item_id, item_info);
            -- 设置其他属性
            dirui_view.generate_zone_item_special(dw_item_id, item_info);
        else
            base_utility.log("[dirui_view]generate_zone_item, dw_item_id is illegal");
        end;
    end;

    -- 更新area ui
    update_area_list_ui = function()
        if #(dirui_model.area_list_toshow) > 0 then
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.area_UO, "Visible", "true");
            -- 设置area区域高度
            local area_num = #(dirui_model.area_list_toshow);
            local area_height = math.ceil(area_num/4) * 56;
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.area_UO, "Height", area_height);
            -- 更新内容
            dirui_view.generate_area_list();
        else
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.area_UO, "Height", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.area_UO, "Visible", "false");
        end;
    end;

    generate_area_list = function()
        local dw_area_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.area_UO);
        if dirui_view.is_valid_id(dw_area_uo) == false then
            base_utility.log("[dirui_view]generate_area_list, dw_area_uo is illegal");
            return;
        end;
        
        dirui_view.p_area_list = dirui_view.generate_common_table(dirui_view.p_area_list, dw_area_uo, dirui_view_ui_path.area_list, "wegame_dir_ui\\common_item.tpf/TenioUILayout/UIObject/", 4, 175, dirui_model.area_list_toshow, "area");
    end;

    generate_area_item = function(dw_item_id, item_info)
        if dirui_view.is_valid_id(dw_item_id) then
            -- 设置名称
           dirui_view.generate_item_name(dw_item_id, item_info);
        else
            base_utility.log("[dirui_view]generate_area_item, dw_item_id is illegal");
        end;
    end;

    -- 更新isp ui
    update_isp_list_ui = function()
        if #(dirui_model.isp_list_toshow) > 0 then
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.isp_UO, "Visible", "true");
            dirui_view.set_isp_list_height();
            dirui_view.generate_isp_list();
        else
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.isp_UO, "Height", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.isp_UO, "Visible", "false");
        end;
    end;

    generate_isp_list = function()
        local dw_isp_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.isp_UO);
        if dirui_view.is_valid_id(dw_isp_uo) == false then
            base_utility.log("[dirui_view]generate_isp_list, dw_isp_uo is illegal");
            return;
        end;
        -- 生成大区列表
        local isp_num = #(dirui_model.isp_list_toshow);
        dirui_view.p_isp_list = dirui_view.generate_common_table(dirui_view.p_isp_list, dw_isp_uo, dirui_view_ui_path.isp_list, "wegame_dir_ui\\isp_item.tpf/TenioUILayout/UIObject/", isp_num, 57, dirui_model.isp_list_toshow, "isp");
    end;

    generate_isp_item = function(dw_item_id, item_info)
        if dirui_view.is_valid_id(dw_item_id) then
            -- 根据名字设置宽度
            local this_width = ui_utility.cal_str_width(item_info.title) + 16;
            ui_utility.setproperty_by_id(dw_item_id, "Width", this_width);
            -- 设置名称
            ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.isp_item, "Caption", item_info.title);
            -- 设置是否选中
            if item_info.is_selected then
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.isp_item, "Background", dirui_view_constant.btn_isp_selected_bk);
            else
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.isp_item, "Background", dirui_view_constant.btn_isp_normal_bk);
            end;
        else
            base_utility.log("[dirui_view]generate_isp_item, dw_item_id is illegal");
        end;
    end;

    -- 生成table列表，并插入内容
    generate_common_table = function(p_list, father_id, table_path, table_item_path, colum_num, colum_width, show_list, list_type)
        if p_list == nil then
            p_list = tolua.cast(ui_utility.get_ctrl_ptr_by_path(father_id, table_path), "Tenio::ITable");
        end;

        if p_list ~= nil then
            -- 遍历添加item
            if dirui_view.is_reopen_by_new_game == true then
                local col_num = p_list:GetColumnCount();
                local is_succ = false;
                if col_num > 0 then
                    for i = 1, col_num do
                        is_succ = p_list:DeleteColumn(0, FALSE);
                    end;
                end;
            end;
            
            if p_list:GetColumnCount() == 0 then
                -- 插入布局文件
                local const_ui_str, str_holder = U8toCUIStr(table_item_path);
                local const_null_str, str_null_holder = U8toCUIStr("");
                local append_result = 0;
                for i = 1, colum_num do
                    if list_type == "isp" then
                        -- 如果是isp，则根据文字长度设置列宽度
                        local title = show_list[i].title;
                        local this_width = ui_utility.cal_str_width(title) + 12;
                        append_result = p_list:AppendCustomColumn(this_width, const_ui_str, const_null_str, 0);
                    else
                        append_result = p_list:AppendCustomColumn(colum_width, const_ui_str, const_null_str, 0);
                    end;
                    
                end;
            end;
            p_list:DeleteAll();
            local row_num = math.ceil(#(show_list)/4);
            if list_type == "isp" then
                row_num = 1;
            end;
            p_list:AppendRowCount(row_num, FALSE);
            for row = 1, row_num do
                for col = 1, colum_num do
                    local p_item_root = p_list:GetItemTopControlPtr(row - 1, col - 1);
                    local dw_item_root = p_item_root:GetControlID();
                    local item_index = (row - 1) * 4 + col;
                    if item_index > 0 and item_index <= #(show_list) then
                        show_list[item_index].item_id = dw_item_root;
                        if list_type == "history" then
                            dirui_view.generate_history_item(dw_item_root, show_list[item_index]);
                        elseif list_type == "isp" then
                            dirui_view.generate_isp_item(dw_item_root, show_list[item_index]);
                        elseif list_type == "area" then
                            dirui_view.generate_area_item(dw_item_root, show_list[item_index]);
                        elseif list_type == "zone" then
                            dirui_view.generate_zone_item(dw_item_root, show_list[item_index]);
                        else
                            base_utility.log("[generate_common_table], list_type is illegal");
                        end;
                        p_item_root:Show(TRUE);
                    else
                        p_item_root:Show(FALSE);
                    end;
                    
                end;
            end;
        else
            base_utility.log("[dirui_view]generate_common_table, p_list is nil");
        end;
        return p_list;
    end;

    -- 生成table item的名称
    generate_item_name = function(dw_item_id, item_info)
        if dirui_view.is_valid_id(dw_item_id) then
            -- 设置名称
            ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_name, "Caption", item_info.title);
            -- 设置是否选中
            if item_info.is_selected then
                -- 设置选中背景
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_btn, "Background", dirui_view_constant.btn_selected_bk);
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_btn, "BackgroundHover", dirui_view_constant.btn_selected_bk);
            else
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_btn, "Background", dirui_view_constant.btn_normal_bk);
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_btn, "BackgroundHover", dirui_view_constant.btn_hover_bk);
            end;
        else
            base_utility.log("[dirui_view]generate_zone_item, dw_item_id is illegal");
        end;
    end;

    -- 生成大区的特殊属性
    generate_zone_item_special = function(dw_item_id, item_info)
        if dirui_view.is_valid_id(dw_item_id) then
            -- 设置网络状态
            local server_icon = "NONE:";
            if item_info.server_status >= 0 then
                server_ico = dirui_view_constant.server_tatus_icon[item_info.server_status + 1];
            end;
            ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_server_icon, "Background", server_ico);
            ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_server_icon, "Visible", "true");

            -- 设置推荐 or hot
            if item_info.hot_flag == 1 then
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_suggest_icon, "Background", dirui_view_constant.hot_bk_icon);
            elseif item_info.new_flag == 1 then
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_suggest_icon, "Background", dirui_view_constant.new_bk_icon);
            else
                if item_info.picked_flag == 1 then
                    ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_suggest_icon, "Background", dirui_view_constant.suggest_bk_icon)
                else
                    ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_suggest_icon, "Background", "NONE:")
                end;
            end;

            -- 设置加锁状态
            if item_info.is_lock == true then
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_lock_icon, "Visible", "true");
                -- 设置ping值不显示
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_ping, "Visible", "false");
            else
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_lock_icon, "Visible", "false");
            end;
            -- 设置ping值
            if dirui_model.show_ping_value == true then
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_ping, "Visible", "true");
            else
                ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.common_item_ping, "Visible", "false");
            end;

        else
            base_utility.log("[dirui_view]generate_zone_item_special, dw_item_id is illegal");
        end;
    end;

    -- 显示网络状态提示
    show_status_tip_list = function()
        local p_status_list = ui_utility.get_ctrl_ptr_by_path(dirui_view.dw_main_id, dirui_view_ui_path.status_List);
        if p_status_list ~= nil then
            if p_status_list:GetColumnCount() == 0 then
                -- 插入布局文件
                local const_ui_str, str_holder = U8toCUIStr("wegame_dir_ui\\status_item.tpf/TenioUILayout/UIObject/");
                local const_null_str, str_null_holder = U8toCUIStr("");
                local append_result = 0;
                for i = 1, 4 do
                    append_result = p_status_list:AppendCustomColumn(68, const_ui_str, const_null_str, 0);
                end;
            end;
            p_status_list:DeleteAll();
            p_status_list:AppendRowCount(1, false);
            local p_green = p_status_list:GetItemTopControlPtr(0, 0);
            local dw_green_id = p_green:GetControlID();
            dirui_view.generate_status_tip_item(dw_green_id, "client_dirui\\server_green.png", LoadStr("DIR_STATUS_GREEN"));
            local p_yellow = p_status_list:GetItemTopControlPtr(0, 1);
            local dw_yellow_id = p_yellow:GetControlID();
            dirui_view.generate_status_tip_item(dw_yellow_id, "client_dirui\\server_yellow.png", LoadStr("DIR_STATUS_YELLOW"));
            local p_red = p_status_list:GetItemTopControlPtr(0, 2);
            local dw_red_id = p_red:GetControlID();
            dirui_view.generate_status_tip_item(dw_red_id, "client_dirui\\server_red.png", LoadStr("DIR_STATUS_RED"));
            local p_unuse = p_status_list:GetItemTopControlPtr(0, 3);
            local dw_unuse_id = p_unuse:GetControlID();
            dirui_view.generate_status_tip_item(dw_unuse_id, "client_dirui\\server_gray.png", LoadStr("DIR_STATUS_GRAY"));
        else
            base_utility.log("[dirui_view]show_status_tip_list, get status_list pointer failed");
        end;
    end;

    -- 生成网络状态提示item
    generate_status_tip_item = function(dw_item_id, icon_status, txt_status)
        if dirui_view.is_valid_id(dw_item_id) then
            ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.status_btn, "Icon", icon_status);
            ui_utility.setproperty_by_path(dw_item_id, dirui_view_ui_path.status_btn, "Caption", txt_status);
        else
            base_utility.log("[dirui_view]generate_status_tip_item, dw_item_id is illegal");
        end;
    end;

    -- 设置滚动区域高度
    set_scroll_container_height = function()
        -- 获得历史大区区域高度
        local history_height = 0;
        if #(dirui_model.h_info_list) > 0 or #(dirui_model.picked_info_list) > 0 then
            history_height = 86;
        end;
        -- 获得ips区域的高度
        local isp_height = 0;
        if #(dirui_model.isp_list_toshow) > 0 then
            isp_height = 30;
        end;

        -- 获得area区域的高度
        local area_height = 0;
        if #(dirui_model.area_list_toshow) > 0 then
            local area_row_num = math.ceil(#(dirui_model.area_list_toshow)/4);
            area_height = area_row_num * dirui_view_constant.btn_height;
        end;

        -- 获得zone区域高度
        local zone_height = 0;
        if #(dirui_model.zone_list_toshow) > 0 then
            local zone_row_num = math.ceil(#(dirui_model.zone_list_toshow)/4);
            if #(dirui_model.area_list_toshow) > 0 then
                zone_height = zone_row_num * dirui_view_constant.btn_height + 48;
            elseif dirui_control.should_show_horz_zone_list() then
                zone_height = zone_row_num * dirui_view_constant.zone_horz_item_height;
            else
                zone_height = zone_row_num * dirui_view_constant.btn_height;
            end;
            
        end;

        local total_height = 30 + history_height + 30 + isp_height + 10 + area_height + 30 + zone_height;

        ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.scroll_container, "Height", total_height);

    end;

    -- 设置isp的YOffset
    set_isp_list_height = function()
        local history_num = 0;
        if #(dirui_model.h_info_list) > 0 then
            history_num = #(dirui_model.h_info_list);
        elseif #(dirui_model.picked_info_list) > 0 then
            history_num = #(dirui_model.picked_info_list);
        else
            history_num = 0;
        end;
        local offset_y = 60;
        local isp_height = 30;
        if history_num > 0 then
            offset_y = offset_y + 86;
        end;
        ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.isp_UO, "YOffset", offset_y);
        ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.isp_UO, "Height", 30);
    end;

    set_area_list_height = function()
        -- body
    end;

    -- 设置zone列表的高度和offsetY
    set_zone_height = function ()
        local area_num = #(dirui_model.area_list_toshow);
        local history_num = 0;
        if #(dirui_model.h_info_list) > 0 then
            history_num = #(dirui_model.h_info_list);
        elseif #(dirui_model.picked_info_list) > 0 then
            history_num = #(dirui_model.picked_info_list);
        else
            history_num = 0;
        end;
        local area_height = math.ceil(area_num/4) * 56;
        local offset_y = 30;
        if history_num > 0 then
            offset_y = offset_y + 86;
        end;
        local isp_num = #(dirui_model.isp_list_toshow);
        if isp_num > 0 then
            offset_y = offset_y + 70;
        end;
        offset_y = offset_y + area_height;
        if area_height > 0 then
            offset_y = offset_y + 30;
        end;
        
        
        local zone_num = #(dirui_model.zone_list_toshow);
        local zone_height = math.ceil(zone_num/4) * dirui_view_constant.btn_height;
        ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "YOffset", offset_y);
        

        -- 设置zone区域title
        local dw_zone_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO);
        if area_num == 0 then
            if dirui_model.dir_tree_height < 2 then
                ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title, "Caption", LoadStr("DIR_ISP_TITLE"));
                -- 设置显示文本
                ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Height", zone_height + 48);
                ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title, "Visible", "true");
            else
                ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title, "Visible", "false");
                 ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Height", zone_height + 2);
            end;
        else
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title, "Visible", "true");
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title, "YOffset", 0);
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title, "Caption", LoadStr("DIRUI_SELECT_SERVER"));
            -- 设置显示文本
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Height", zone_height + 48);
        end;
        -- 设置zone list 位置
        if area_num == 0 then
            if dirui_model.dir_tree_height < 2 then
                ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_list_container, "YOffset", 40);
            else
                ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_list_container, "YOffset", 0);
            end;
            
        else
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_list_container, "YOffset", 40);
        end;
        ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_list_container, "Height", zone_height + 6);

    end;

    -- 计算加锁tips位置和内容
    cal_lock_tips_pos = function(dw_hover_id, show_big)
        local p_btn_hover = ui_utility.get_ctrl_ptr_by_id(dw_hover_id);
        local p_main = ui_utility.get_ctrl_ptr_by_id(dirui_view.dw_main_id);
        local p_btn_tips = nil;
        if show_big == true then
            p_btn_tips = ui_utility.get_ctrl_ptr_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn);
        else
            p_btn_tips = ui_utility.get_ctrl_ptr_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn);
        end;
        local hover_btn_rect = nil;
        local main_ui_rect = nil;
        local tips_btn_rect = nil;
        if p_btn_hover ~= nil and p_main ~= nil and p_btn_tips ~= nil then
            hover_btn_rect = p_btn_hover:GetRectEx(TRUE,TRUE);
            main_ui_rect = p_main:GetRectEx(TRUE,TRUE);
            tips_btn_rect = p_btn_tips:GetRectEx(TRUE,TRUE);
        end;
        -- 计算hover tips要放置的位置
        local x_offset = hover_btn_rect.left - main_ui_rect.left + (hover_btn_rect.right - hover_btn_rect.left) / 2 -(tips_btn_rect.right - tips_btn_rect.left) / 2;
        local y_offset = hover_btn_rect.top - main_ui_rect.top - (tips_btn_rect.bottom - tips_btn_rect.top);
        if show_big == true then
            -- 设置文本
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_content, "Caption", dirui_model.lol_tips_data.main_tips);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn, "XOffset", x_offset);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn, "YOffset", y_offset);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn, "Visible", "true");
            
        else
            
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn, "XOffset", x_offset);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn, "YOffset", y_offset);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn, "Visible", "true");
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_content, "Caption", dirui_model.lol_tips_data.sub_tips);
        end;
        
    end;

    -- 重置加锁tips位置和内容
    reset_lock_tip_pos = function(dw_hover_id, show_big)
        if show_big == true then
            -- 设置文本
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_content, "Caption", "");
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn, "XOffset", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn, "YOffset", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_big_btn, "Visible", "false");
        else
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_content, "Caption", "");
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn, "XOffset", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn, "YOffset", 0);
            ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.hover_small_btn, "Visible", "false");
        end;
    end;

    update_ping_value = function(update_list)
        if #(dirui_model.ping_value_list) > 0 then
            for i = 1, #(dirui_model.ping_value_list) do
                local temp_info = dirui_model.ping_value_list[i];
                local ping_value = 0;
                if temp_info.ping ~= nil then
                    ping_value = tonumber(temp_info.ping);
                end;
                if ping_value > 0 then
                    if ping_value > 1000 then
                        -- 此种情况是代表无加速链路，根据产品要求，此处改为默认显示40
                        ping_value = 40;
                    end;
                    ping_value = tostring(ping_value).."ms";
                    for j = 1, #(update_list) do
                        if update_list[j].zone_id == temp_info.zone_id then
                            -- 找到当前ping值对应的zone信息
                            local parent_id = update_list[j].item_id;
                            if dirui_view.is_valid_id(parent_id) and update_list[j].is_lock == false then
                                ui_utility.setproperty_by_path(parent_id, dirui_view_ui_path.common_item_ping, "Visible", "true");
                                ui_utility.setproperty_by_path(parent_id, dirui_view_ui_path.common_item_ping, "Caption", ping_value);
                            else
                                base_utility.log("[dirui_view]update_ping_value, parent_id is illegal");
                            end;
                            break;
                        end;
                    end;
                end;
            end;
        else
            base_utility.log("[dirui_view]");
        end;
    end;

    -- 更新zone节点的选中态
    update_select_status_zone = function()
        dirui_view.update_select_status_common(dirui_model.zone_list_toshow, dirui_model.selected_zone_index);
    end;

    -- 更新area节点的选中态
    update_select_status_area = function()
        dirui_view.update_select_status_common(dirui_model.area_list_toshow, dirui_model.selected_area_index);
    end;

    -- 更新history节点选中态
    update_select_status_history = function()
        if #(dirui_model.h_info_list) > 0 then
            dirui_view.update_select_status_common(dirui_model.h_info_list, dirui_model.selected_zone_index);
        elseif #(dirui_model.picked_info_list) > 0 then
            dirui_view.update_select_status_common(dirui_model.picked_info_list, dirui_model.selected_zone_index);
        end;
    end;

    update_select_status_common = function(info_list, selected_index)
        for i = 1, #(info_list) do
            if info_list[i].index == selected_index then
                local table_item_id = info_list[i].item_id;
                -- 查找button
                ui_utility.setproperty_by_path(table_item_id, dirui_view_ui_path.common_item_btn, "Background", dirui_view_constant.btn_selected_bk);
                ui_utility.setproperty_by_path(table_item_id, dirui_view_ui_path.common_item_btn, "BackgroundHover", dirui_view_constant.btn_selected_bk);
            else
                local table_item_id = info_list[i].item_id;
                -- 查找button
                ui_utility.setproperty_by_path(table_item_id, dirui_view_ui_path.common_item_btn, "Background", dirui_view_constant.btn_normal_bk);
                ui_utility.setproperty_by_path(table_item_id, dirui_view_ui_path.common_item_btn, "BackgroundHover", dirui_view_constant.btn_hover_bk);
            end;
        end;
    end;

    update_select_status_isp = function()
        for i = 1, #(dirui_model.isp_list_toshow) do
            if dirui_model.isp_list_toshow[i].index == dirui_model.selected_isp_index then
                local table_item_id = dirui_model.isp_list_toshow[i].item_id;
                -- 查找button
                ui_utility.setproperty_by_path(table_item_id, dirui_view_ui_path.isp_item, "Background", dirui_view_constant.btn_isp_selected_bk);
            else
                local table_item_id = dirui_model.isp_list_toshow[i].item_id;
                -- 查找button
                ui_utility.setproperty_by_path(table_item_id, dirui_view_ui_path.isp_item, "Background", dirui_view_constant.btn_isp_normal_bk);
            end;
        end;
    end;

    -- 控件ID是否有效
    is_valid_id = function(dw_ctrl)
        return (nil ~= dw_ctrl) and (0 ~= dw_ctrl);
    end;

    show_vert_zone_list = function(show)
        local show_str = "true";
        if not show then
            show_str = "false";
        end
        -- isp list
        local dw_isp_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, 
                                        dirui_view_ui_path.isp_UO);
        ui_utility.setproperty_by_path(dw_isp_uo, dirui_view_ui_path.isp_list_container,
                                        "Visible", show_str);
        -- original zonelist
        local dw_zone_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, 
                                        dirui_view_ui_path.zone_UO);
        ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_list_container,
                                        "Visible", show_str);
        ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_title,
                                        "Visible", show_str);
        if show then
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_horz_list,
                                        "Visible", "false");
        end
    end;

    set_horz_zone_height = function()
        local history_num = 0;
        if #(dirui_model.h_info_list) > 0 then
            history_num = #(dirui_model.h_info_list);
        elseif #(dirui_model.picked_info_list) > 0 then
            history_num = #(dirui_model.picked_info_list);
        else
            history_num = 0;
        end;
        local offset_y = 30;
        if history_num > 0 then
            offset_y = offset_y + 86;
        end;
        local isp_num = #(dirui_model.isp_list_toshow);
        if isp_num > 0 then
            offset_y = offset_y + 70;
        end;
        
        local zone_num = #(dirui_model.zone_list_toshow);
        local zone_height = math.ceil(zone_num/4) * dirui_view_constant.zone_horz_item_height;
        ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "YOffset", offset_y);
        
        local dw_zone_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO);
        ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_horz_list, "Visible", "true");

        if dirui_model.dir_tree_height < 2 then
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_horz_list, "YOffset", 40);
        else
            ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_horz_list, "YOffset", 0);
        end;
        ui_utility.setproperty_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO, "Height", zone_height + 6);
        ui_utility.setproperty_by_path(dw_zone_uo, dirui_view_ui_path.zone_horz_list, "Height", zone_height + 6);
    end;

    generate_horz_zone_list = function()
        local dw_zone_uo = ui_utility.get_ctrl_id_by_path(dirui_view.dw_main_id, dirui_view_ui_path.zone_UO);
        if dirui_view.is_valid_id(dw_zone_uo) == false then
            base_utility.log("[dirui_view]generate_zone_list, dw_zone_uo is illegal");
            return;
        end;

        local horz_zlist_ptr = ui_utility.get_ctrl_ptr_by_path(dw_zone_uo, dirui_view_ui_path.zone_horz_list);
        if nil == horz_zlist_ptr then
            return;
        end

        local item_count = #dirui_model.zone_horz_list_toshow;
        horz_zlist_ptr:SetItemCount(item_count);

        for index = 0, item_count - 1 do
            local zlist = dirui_model.zone_horz_list_toshow[index + 1];
            local dw_item_id = horz_zlist_ptr:GetItemRootIDByIdx(index);
            horz_zlist_ptr:SetItemDraggable(index, false);
            local tbl_ptr = ui_utility.get_ctrl_ptr_by_path(dw_item_id, "Table1");
            dirui_view.generate_common_table(tbl_ptr, dw_zone_uo, "",
            dirui_view_ui_path.common_item_layout, 4, 176, zlist,
            "zone");
            -- zone title(isp)
            if #zlist > 0 then
                local isp_idx = zlist[1].index;
                if nil ~= isp_idx and isp_idx > 0 and isp_idx <= #dirui_model.all_dir_info_list then
                    isp_idx = dirui_model.all_dir_info_list[isp_idx].parentInx;
                end
                if nil ~= isp_idx and isp_idx >= 0 and isp_idx < #dirui_model.all_dir_info_list then
                    local title = dirui_model.all_dir_info_list[isp_idx + 1].title or "";
                    if "" ~= title then
                        local label_ctrl = ui_utility.get_ctrl_id_by_path(dw_item_id, "zone_name");
                        ui_utility.setproperty_by_id(label_ctrl, "Caption", title);
                    end
                end
            end
        end

    end;
}

dirui_view.init();